<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-brand">
        <div class="brand-logo">
          <img src="/favicon.ico" alt="Logo" class="logo-image" />
        </div>
        <h1 class="brand-title">ReactIM</h1>
        <p class="brand-description">实时通讯，连接你我</p>
      </div>

      <LoginForm
        :auto-focus="true"
        @login-success="handleLoginSuccess"
        @login-error="handleLoginError"
      />
    </div>

    <!-- Background decoration -->
    <div class="login-background">
      <div class="bg-shape bg-shape-1"></div>
      <div class="bg-shape bg-shape-2"></div>
      <div class="bg-shape bg-shape-3"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import LoginForm from '@/components/LoginForm.vue'
import { useAuthStore } from '@/stores/auth'

// Router
const router = useRouter()

// Store
const authStore = useAuthStore()

// Methods
const handleLoginSuccess = () => {
  // Redirect to main chat page after successful login
  router.push({ name: 'chat' })
}

const handleLoginError = (error: string) => {
  ElMessage.error(error || '登录失败，请重试')
}

// Check if user is already authenticated
onMounted(() => {
  if (authStore.isAuthenticated) {
    // User is already logged in, redirect to chat
    router.push({ name: 'chat' })
  }
})
</script>

<style scoped lang="scss">
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  padding: 1rem;

  @media (max-width: 768px) {
    padding: 0;
    align-items: flex-start;
  }
}

.login-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 400px;

  @media (max-width: 768px) {
    max-width: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 2rem 1rem;
  }
}

.login-brand {
  text-align: center;
  margin-bottom: 2rem;
  color: white;

  .brand-logo {
    margin-bottom: 1rem;

    .logo-image {
      width: 64px;
      height: 64px;
      border-radius: 12px;
      background: rgba(255, 255, 255, 0.1);
      padding: 8px;
      backdrop-filter: blur(10px);
    }
  }

  .brand-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0 0 0.5rem 0;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  }

  .brand-description {
    font-size: 1rem;
    opacity: 0.9;
    margin: 0;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  }

  @media (max-width: 768px) {
    margin-bottom: 3rem;

    .brand-title {
      font-size: 1.75rem;
    }

    .brand-description {
      font-size: 0.875rem;
    }
  }
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  z-index: 1;
}

.bg-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  animation: float 6s ease-in-out infinite;

  &.bg-shape-1 {
    width: 200px;
    height: 200px;
    top: 10%;
    left: 10%;
    animation-delay: 0s;
  }

  &.bg-shape-2 {
    width: 150px;
    height: 150px;
    top: 60%;
    right: 10%;
    animation-delay: 2s;
  }

  &.bg-shape-3 {
    width: 100px;
    height: 100px;
    bottom: 20%;
    left: 20%;
    animation-delay: 4s;
  }

  @media (max-width: 768px) {
    &.bg-shape-1 {
      width: 120px;
      height: 120px;
    }

    &.bg-shape-2 {
      width: 80px;
      height: 80px;
    }

    &.bg-shape-3 {
      width: 60px;
      height: 60px;
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

// Dark mode support
@media (prefers-color-scheme: dark) {
  .login-page {
    background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  }

  .bg-shape {
    background: rgba(255, 255, 255, 0.05);
  }
}
</style>